{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "95a922dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "import pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "910f8867",
   "metadata": {},
   "source": [
    "# pandas时间序列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56b52e2b",
   "metadata": {},
   "source": [
    "**时间戳**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "06b42857",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2030-02-13', '2030-02-14', '2030-02-15', '2030-02-16'], dtype='datetime64[ns]', freq='D')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2030-02-13', '2030-02-14', '2030-02-15', '2030-02-16'], dtype='period[D]')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 创建一个时间戳\n",
    "pandas.Timestamp('2030-2-3') # 时刻数据\n",
    "pandas.Period('2030-2-3',freq='D') # 时期数据\n",
    "# freq  Y:年  M:月  D:日\n",
    "\n",
    "# 批量生成时刻数据\n",
    "index1 = pandas.date_range(start='2030.02.13',periods=4,freq='D') # period 表示要创建多少个时间\n",
    "\n",
    "# 批量生成时期数据\n",
    "index2 = pandas.period_range(start='2030.02.13',periods=4,freq='D')\n",
    "\n",
    "display(index1,index2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd2e50f1",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d7d811ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1970-01-22 23:41:18.987000'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换方法\n",
    "pandas.to_datetime(['2030.03.14',])\n",
    "\n",
    "# 时间戳 -> 时间\n",
    "pandas.to_datetime([1899678987],unit='s')\n",
    "\n",
    "pandas.to_datetime([1899678987],unit='ms')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "20f6504a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1970-01-23 23:41:18.987000'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = pandas.to_datetime([1899678987],unit='ms')\n",
    "dt + pandas.DateOffset(hours=8)\n",
    "dt + pandas.DateOffset(days=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44151827",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "acf16612",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pandas.date_range(start='2030.3.20',periods=100,freq='D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7b279f0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-20     0\n",
       "2030-03-21     1\n",
       "2030-03-22     2\n",
       "2030-03-23     3\n",
       "2030-03-24     4\n",
       "              ..\n",
       "2030-06-23    95\n",
       "2030-06-24    96\n",
       "2030-06-25    97\n",
       "2030-06-26    98\n",
       "2030-06-27    99\n",
       "Freq: D, Length: 100, dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pandas.Series(range(len(index)),index=index)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "7e27b3eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-20     0\n",
       "2030-03-21     1\n",
       "2030-03-22     2\n",
       "2030-03-23     3\n",
       "2030-03-24     4\n",
       "2030-03-25     5\n",
       "2030-03-26     6\n",
       "2030-03-27     7\n",
       "2030-03-28     8\n",
       "2030-03-29     9\n",
       "2030-03-30    10\n",
       "2030-03-31    11\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引\n",
    "ts['2030.3.23'] # 日\n",
    "ts['2030.3'] # 3月份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "2248d922",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-23     3\n",
       "2030-03-24     4\n",
       "2030-03-25     5\n",
       "2030-03-26     6\n",
       "2030-03-27     7\n",
       "2030-03-28     8\n",
       "2030-03-29     9\n",
       "2030-03-30    10\n",
       "2030-03-31    11\n",
       "2030-04-01    12\n",
       "2030-04-02    13\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "ts['2030.3.23':'2030.4.2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "297a186f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4,\n",
       "       5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0,\n",
       "       1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3,\n",
       "       4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6,\n",
       "       0, 1, 2, 3],\n",
       "      dtype='int32')"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 属性\n",
    "ts.index\n",
    "ts.index.year # 取到所有的年\n",
    "ts.index.dayofweek # 星期几"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb0ce39c",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "736b428e",
   "metadata": {},
   "source": [
    "**时间序列中常用的方法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "6a1a8611",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pandas.date_range('2030.3.1',periods=365,freq='D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "0f030a5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01    322\n",
       "2030-03-02    112\n",
       "2030-03-03    323\n",
       "2030-03-04    471\n",
       "2030-03-05    316\n",
       "             ... \n",
       "2031-02-24    493\n",
       "2031-02-25    410\n",
       "2031-02-26    192\n",
       "2031-02-27    443\n",
       "2031-02-28    105\n",
       "Freq: D, Length: 365, dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ts = pandas.Series(numpy.random.randint(0,500,len(index)),index=index)\n",
    "display(ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b2a626df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01      NaN\n",
       "2030-03-02      NaN\n",
       "2030-03-03      NaN\n",
       "2030-03-04    322.0\n",
       "2030-03-05    112.0\n",
       "              ...  \n",
       "2031-02-24    440.0\n",
       "2031-02-25    155.0\n",
       "2031-02-26    377.0\n",
       "2031-02-27    493.0\n",
       "2031-02-28    410.0\n",
       "Freq: D, Length: 365, dtype: float64"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 移动\n",
    "ts.shift() # 默认往后移动1位\n",
    "ts.shift(periods=3) # 往后移动3位，如果想往前移就填负数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "3282bed9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01    322\n",
       "2030-04-01    412\n",
       "2030-05-01     35\n",
       "2030-06-01    192\n",
       "2030-07-01    409\n",
       "2030-08-01    218\n",
       "2030-09-01     34\n",
       "2030-10-01    278\n",
       "2030-11-01    402\n",
       "2030-12-01    327\n",
       "2031-01-01     45\n",
       "2031-02-01     44\n",
       "Freq: MS, dtype: int32"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 频率的转换\n",
    "ts.asfreq(pandas.tseries.offsets.Week()) # 按星期变化，原来是按照天变化\n",
    "ts.asfreq(pandas.tseries.offsets.MonthBegin()) # 这里按月变化，从月的第一天开始"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d139321",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa9902ac",
   "metadata": {},
   "source": [
    "**重采样**\n",
    "- resample：根据日期维度进行数据聚合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "84aabeca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-31     8118\n",
       "2030-05-31    15341\n",
       "2030-07-31    15483\n",
       "2030-09-30    13316\n",
       "2030-11-30    13714\n",
       "2031-01-31    14818\n",
       "2031-03-31     7094\n",
       "Freq: 2M, dtype: int32"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重采样，resample，分钟T  小时H  日D  周W  月M  年Y\n",
    "ts.resample(rule='2M').sum() # 以每2个月为单位进行汇总"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8df3cdbb",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e72b990",
   "metadata": {},
   "source": [
    "**时区**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "26a279ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# tz就是timezone的意思，时区的意思\n",
    "import pytz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d9dd6dfb",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul', 'Africa/Bissau', 'Africa/Blantyre', 'Africa/Brazzaville', 'Africa/Bujumbura', 'Africa/Cairo', 'Africa/Casablanca', 'Africa/Ceuta', 'Africa/Conakry', 'Africa/Dakar', 'Africa/Dar_es_Salaam', 'Africa/Djibouti', 'Africa/Douala', 'Africa/El_Aaiun', 'Africa/Freetown', 'Africa/Gaborone', 'Africa/Harare', 'Africa/Johannesburg', 'Africa/Juba', 'Africa/Kampala', 'Africa/Khartoum', 'Africa/Kigali', 'Africa/Kinshasa', 'Africa/Lagos', 'Africa/Libreville', 'Africa/Lome', 'Africa/Luanda', 'Africa/Lubumbashi', 'Africa/Lusaka', 'Africa/Malabo', 'Africa/Maputo', 'Africa/Maseru', 'Africa/Mbabane', 'Africa/Mogadishu', 'Africa/Monrovia', 'Africa/Nairobi', 'Africa/Ndjamena', 'Africa/Niamey', 'Africa/Nouakchott', 'Africa/Ouagadougou', 'Africa/Porto-Novo', 'Africa/Sao_Tome', 'Africa/Tripoli', 'Africa/Tunis', 'Africa/Windhoek', 'America/Adak', 'America/Anchorage', 'America/Anguilla', 'America/Antigua', 'America/Araguaina', 'America/Argentina/Buenos_Aires', 'America/Argentina/Catamarca', 'America/Argentina/Cordoba', 'America/Argentina/Jujuy', 'America/Argentina/La_Rioja', 'America/Argentina/Mendoza', 'America/Argentina/Rio_Gallegos', 'America/Argentina/Salta', 'America/Argentina/San_Juan', 'America/Argentina/San_Luis', 'America/Argentina/Tucuman', 'America/Argentina/Ushuaia', 'America/Aruba', 'America/Asuncion', 'America/Atikokan', 'America/Bahia', 'America/Bahia_Banderas', 'America/Barbados', 'America/Belem', 'America/Belize', 'America/Blanc-Sablon', 'America/Boa_Vista', 'America/Bogota', 'America/Boise', 'America/Cambridge_Bay', 'America/Campo_Grande', 'America/Cancun', 'America/Caracas', 'America/Cayenne', 'America/Cayman', 'America/Chicago', 'America/Chihuahua', 'America/Ciudad_Juarez', 'America/Costa_Rica', 'America/Creston', 'America/Cuiaba', 'America/Curacao', 'America/Danmarkshavn', 'America/Dawson', 'America/Dawson_Creek', 'America/Denver', 'America/Detroit', 'America/Dominica', 'America/Edmonton', 'America/Eirunepe', 'America/El_Salvador', 'America/Fort_Nelson', 'America/Fortaleza', 'America/Glace_Bay', 'America/Goose_Bay', 'America/Grand_Turk', 'America/Grenada', 'America/Guadeloupe', 'America/Guatemala', 'America/Guayaquil', 'America/Guyana', 'America/Halifax', 'America/Havana', 'America/Hermosillo', 'America/Indiana/Indianapolis', 'America/Indiana/Knox', 'America/Indiana/Marengo', 'America/Indiana/Petersburg', 'America/Indiana/Tell_City', 'America/Indiana/Vevay', 'America/Indiana/Vincennes', 'America/Indiana/Winamac', 'America/Inuvik', 'America/Iqaluit', 'America/Jamaica', 'America/Juneau', 'America/Kentucky/Louisville', 'America/Kentucky/Monticello', 'America/Kralendijk', 'America/La_Paz', 'America/Lima', 'America/Los_Angeles', 'America/Lower_Princes', 'America/Maceio', 'America/Managua', 'America/Manaus', 'America/Marigot', 'America/Martinique', 'America/Matamoros', 'America/Mazatlan', 'America/Menominee', 'America/Merida', 'America/Metlakatla', 'America/Mexico_City', 'America/Miquelon', 'America/Moncton', 'America/Monterrey', 'America/Montevideo', 'America/Montserrat', 'America/Nassau', 'America/New_York', 'America/Nome', 'America/Noronha', 'America/North_Dakota/Beulah', 'America/North_Dakota/Center', 'America/North_Dakota/New_Salem', 'America/Nuuk', 'America/Ojinaga', 'America/Panama', 'America/Paramaribo', 'America/Phoenix', 'America/Port-au-Prince', 'America/Port_of_Spain', 'America/Porto_Velho', 'America/Puerto_Rico', 'America/Punta_Arenas', 'America/Rankin_Inlet', 'America/Recife', 'America/Regina', 'America/Resolute', 'America/Rio_Branco', 'America/Santarem', 'America/Santiago', 'America/Santo_Domingo', 'America/Sao_Paulo', 'America/Scoresbysund', 'America/Sitka', 'America/St_Barthelemy', 'America/St_Johns', 'America/St_Kitts', 'America/St_Lucia', 'America/St_Thomas', 'America/St_Vincent', 'America/Swift_Current', 'America/Tegucigalpa', 'America/Thule', 'America/Tijuana', 'America/Toronto', 'America/Tortola', 'America/Vancouver', 'America/Whitehorse', 'America/Winnipeg', 'America/Yakutat', 'Antarctica/Casey', 'Antarctica/Davis', 'Antarctica/DumontDUrville', 'Antarctica/Macquarie', 'Antarctica/Mawson', 'Antarctica/McMurdo', 'Antarctica/Palmer', 'Antarctica/Rothera', 'Antarctica/Syowa', 'Antarctica/Troll', 'Antarctica/Vostok', 'Arctic/Longyearbyen', 'Asia/Aden', 'Asia/Almaty', 'Asia/Amman', 'Asia/Anadyr', 'Asia/Aqtau', 'Asia/Aqtobe', 'Asia/Ashgabat', 'Asia/Atyrau', 'Asia/Baghdad', 'Asia/Bahrain', 'Asia/Baku', 'Asia/Bangkok', 'Asia/Barnaul', 'Asia/Beirut', 'Asia/Bishkek', 'Asia/Brunei', 'Asia/Chita', 'Asia/Choibalsan', 'Asia/Colombo', 'Asia/Damascus', 'Asia/Dhaka', 'Asia/Dili', 'Asia/Dubai', 'Asia/Dushanbe', 'Asia/Famagusta', 'Asia/Gaza', 'Asia/Hebron', 'Asia/Ho_Chi_Minh', 'Asia/Hong_Kong', 'Asia/Hovd', 'Asia/Irkutsk', 'Asia/Jakarta', 'Asia/Jayapura', 'Asia/Jerusalem', 'Asia/Kabul', 'Asia/Kamchatka', 'Asia/Karachi', 'Asia/Kathmandu', 'Asia/Khandyga', 'Asia/Kolkata', 'Asia/Krasnoyarsk', 'Asia/Kuala_Lumpur', 'Asia/Kuching', 'Asia/Kuwait', 'Asia/Macau', 'Asia/Magadan', 'Asia/Makassar', 'Asia/Manila', 'Asia/Muscat', 'Asia/Nicosia', 'Asia/Novokuznetsk', 'Asia/Novosibirsk', 'Asia/Omsk', 'Asia/Oral', 'Asia/Phnom_Penh', 'Asia/Pontianak', 'Asia/Pyongyang', 'Asia/Qatar', 'Asia/Qostanay', 'Asia/Qyzylorda', 'Asia/Riyadh', 'Asia/Sakhalin', 'Asia/Samarkand', 'Asia/Seoul', 'Asia/Shanghai', 'Asia/Singapore', 'Asia/Srednekolymsk', 'Asia/Taipei', 'Asia/Tashkent', 'Asia/Tbilisi', 'Asia/Tehran', 'Asia/Thimphu', 'Asia/Tokyo', 'Asia/Tomsk', 'Asia/Ulaanbaatar', 'Asia/Urumqi', 'Asia/Ust-Nera', 'Asia/Vientiane', 'Asia/Vladivostok', 'Asia/Yakutsk', 'Asia/Yangon', 'Asia/Yekaterinburg', 'Asia/Yerevan', 'Atlantic/Azores', 'Atlantic/Bermuda', 'Atlantic/Canary', 'Atlantic/Cape_Verde', 'Atlantic/Faroe', 'Atlantic/Madeira', 'Atlantic/Reykjavik', 'Atlantic/South_Georgia', 'Atlantic/St_Helena', 'Atlantic/Stanley', 'Australia/Adelaide', 'Australia/Brisbane', 'Australia/Broken_Hill', 'Australia/Darwin', 'Australia/Eucla', 'Australia/Hobart', 'Australia/Lindeman', 'Australia/Lord_Howe', 'Australia/Melbourne', 'Australia/Perth', 'Australia/Sydney', 'Canada/Atlantic', 'Canada/Central', 'Canada/Eastern', 'Canada/Mountain', 'Canada/Newfoundland', 'Canada/Pacific', 'Europe/Amsterdam', 'Europe/Andorra', 'Europe/Astrakhan', 'Europe/Athens', 'Europe/Belgrade', 'Europe/Berlin', 'Europe/Bratislava', 'Europe/Brussels', 'Europe/Bucharest', 'Europe/Budapest', 'Europe/Busingen', 'Europe/Chisinau', 'Europe/Copenhagen', 'Europe/Dublin', 'Europe/Gibraltar', 'Europe/Guernsey', 'Europe/Helsinki', 'Europe/Isle_of_Man', 'Europe/Istanbul', 'Europe/Jersey', 'Europe/Kaliningrad', 'Europe/Kirov', 'Europe/Kyiv', 'Europe/Lisbon', 'Europe/Ljubljana', 'Europe/London', 'Europe/Luxembourg', 'Europe/Madrid', 'Europe/Malta', 'Europe/Mariehamn', 'Europe/Minsk', 'Europe/Monaco', 'Europe/Moscow', 'Europe/Oslo', 'Europe/Paris', 'Europe/Podgorica', 'Europe/Prague', 'Europe/Riga', 'Europe/Rome', 'Europe/Samara', 'Europe/San_Marino', 'Europe/Sarajevo', 'Europe/Saratov', 'Europe/Simferopol', 'Europe/Skopje', 'Europe/Sofia', 'Europe/Stockholm', 'Europe/Tallinn', 'Europe/Tirane', 'Europe/Ulyanovsk', 'Europe/Vaduz', 'Europe/Vatican', 'Europe/Vienna', 'Europe/Vilnius', 'Europe/Volgograd', 'Europe/Warsaw', 'Europe/Zagreb', 'Europe/Zurich', 'GMT', 'Indian/Antananarivo', 'Indian/Chagos', 'Indian/Christmas', 'Indian/Cocos', 'Indian/Comoro', 'Indian/Kerguelen', 'Indian/Mahe', 'Indian/Maldives', 'Indian/Mauritius', 'Indian/Mayotte', 'Indian/Reunion', 'Pacific/Apia', 'Pacific/Auckland', 'Pacific/Bougainville', 'Pacific/Chatham', 'Pacific/Chuuk', 'Pacific/Easter', 'Pacific/Efate', 'Pacific/Fakaofo', 'Pacific/Fiji', 'Pacific/Funafuti', 'Pacific/Galapagos', 'Pacific/Gambier', 'Pacific/Guadalcanal', 'Pacific/Guam', 'Pacific/Honolulu', 'Pacific/Kanton', 'Pacific/Kiritimati', 'Pacific/Kosrae', 'Pacific/Kwajalein', 'Pacific/Majuro', 'Pacific/Marquesas', 'Pacific/Midway', 'Pacific/Nauru', 'Pacific/Niue', 'Pacific/Norfolk', 'Pacific/Noumea', 'Pacific/Pago_Pago', 'Pacific/Palau', 'Pacific/Pitcairn', 'Pacific/Pohnpei', 'Pacific/Port_Moresby', 'Pacific/Rarotonga', 'Pacific/Saipan', 'Pacific/Tahiti', 'Pacific/Tarawa', 'Pacific/Tongatapu', 'Pacific/Wake', 'Pacific/Wallis', 'US/Alaska', 'US/Arizona', 'US/Central', 'US/Eastern', 'US/Hawaii', 'US/Mountain', 'US/Pacific', 'UTC']"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看常用的时区有哪些\n",
    "pytz.common_timezones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5bf14c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "5f066a06",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "Already tz-aware, use tz_convert to convert.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[76], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 时区表示\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m ts \u001b[38;5;241m=\u001b[39m ts\u001b[38;5;241m.\u001b[39mtz_localize(tz\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mUTC\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      3\u001b[0m ts\n",
      "File \u001b[1;32mD:\\software\\anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:10568\u001b[0m, in \u001b[0;36mNDFrame.tz_localize\u001b[1;34m(self, tz, axis, level, copy, ambiguous, nonexistent)\u001b[0m\n\u001b[0;32m  10566\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m level \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m (\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m0\u001b[39m, ax\u001b[38;5;241m.\u001b[39mname):\n\u001b[0;32m  10567\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe level \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mlevel\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m is not valid\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m> 10568\u001b[0m     ax \u001b[38;5;241m=\u001b[39m _tz_localize(ax, tz, ambiguous, nonexistent)\n\u001b[0;32m  10570\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcopy(deep\u001b[38;5;241m=\u001b[39mcopy \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m using_copy_on_write())\n\u001b[0;32m  10571\u001b[0m result \u001b[38;5;241m=\u001b[39m result\u001b[38;5;241m.\u001b[39mset_axis(ax, axis\u001b[38;5;241m=\u001b[39maxis, copy\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n",
      "File \u001b[1;32mD:\\software\\anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:10556\u001b[0m, in \u001b[0;36mNDFrame.tz_localize.<locals>._tz_localize\u001b[1;34m(ax, tz, ambiguous, nonexistent)\u001b[0m\n\u001b[0;32m  10554\u001b[0m     ax \u001b[38;5;241m=\u001b[39m DatetimeIndex([], tz\u001b[38;5;241m=\u001b[39mtz)\n\u001b[0;32m  10555\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m> 10556\u001b[0m     ax \u001b[38;5;241m=\u001b[39m ax\u001b[38;5;241m.\u001b[39mtz_localize(tz, ambiguous\u001b[38;5;241m=\u001b[39mambiguous, nonexistent\u001b[38;5;241m=\u001b[39mnonexistent)\n\u001b[0;32m  10557\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m ax\n",
      "File \u001b[1;32mD:\\software\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexes\\datetimes.py:279\u001b[0m, in \u001b[0;36mDatetimeIndex.tz_localize\u001b[1;34m(self, tz, ambiguous, nonexistent)\u001b[0m\n\u001b[0;32m    272\u001b[0m \u001b[38;5;129m@doc\u001b[39m(DatetimeArray\u001b[38;5;241m.\u001b[39mtz_localize)\n\u001b[0;32m    273\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtz_localize\u001b[39m(\n\u001b[0;32m    274\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    277\u001b[0m     nonexistent: TimeNonexistent \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraise\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m    278\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m DatetimeIndex:\n\u001b[1;32m--> 279\u001b[0m     arr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_data\u001b[38;5;241m.\u001b[39mtz_localize(tz, ambiguous, nonexistent)\n\u001b[0;32m    280\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(\u001b[38;5;28mself\u001b[39m)\u001b[38;5;241m.\u001b[39m_simple_new(arr, name\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname)\n",
      "File \u001b[1;32mD:\\software\\anaconda3\\Lib\\site-packages\\pandas\\core\\arrays\\_mixins.py:86\u001b[0m, in \u001b[0;36mravel_compat.<locals>.method\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m     83\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(meth)\n\u001b[0;32m     84\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmethod\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m     85\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m---> 86\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m meth(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m     88\u001b[0m     flags \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_ndarray\u001b[38;5;241m.\u001b[39mflags\n\u001b[0;32m     89\u001b[0m     flat \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mravel(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mK\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32mD:\\software\\anaconda3\\Lib\\site-packages\\pandas\\core\\arrays\\datetimes.py:1035\u001b[0m, in \u001b[0;36mDatetimeArray.tz_localize\u001b[1;34m(self, tz, ambiguous, nonexistent)\u001b[0m\n\u001b[0;32m   1033\u001b[0m         new_dates \u001b[38;5;241m=\u001b[39m tz_convert_from_utc(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39masi8, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtz, reso\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_creso)\n\u001b[0;32m   1034\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m-> 1035\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAlready tz-aware, use tz_convert to convert.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m   1036\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   1037\u001b[0m     tz \u001b[38;5;241m=\u001b[39m timezones\u001b[38;5;241m.\u001b[39mmaybe_get_tz(tz)\n",
      "\u001b[1;31mTypeError\u001b[0m: Already tz-aware, use tz_convert to convert."
     ]
    }
   ],
   "source": [
    "# 时区表示\n",
    "ts = ts.tz_localize(tz='UTC')\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "5ecf948c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01 08:00:00+08:00    322\n",
       "2030-03-02 08:00:00+08:00    112\n",
       "2030-03-03 08:00:00+08:00    323\n",
       "2030-03-04 08:00:00+08:00    471\n",
       "2030-03-05 08:00:00+08:00    316\n",
       "                            ... \n",
       "2031-02-24 08:00:00+08:00    493\n",
       "2031-02-25 08:00:00+08:00    410\n",
       "2031-02-26 08:00:00+08:00    192\n",
       "2031-02-27 08:00:00+08:00    443\n",
       "2031-02-28 08:00:00+08:00    105\n",
       "Freq: D, Length: 365, dtype: int32"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时区转换\n",
    "ts = ts.tz_convert(tz='Asia/ShangHai')\n",
    "ts"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
